home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13221 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  48 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: 101513.2141@compuserve.com (Jean-Marc Delforge)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Date: Fri, 05 Apr 1996 09:33:47 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4k2lu3$9oe@dub-news-svc-1.compuserve.com>
  8. References: <31616F63.481D@lava.weeg.uiowa.edu> <4jtddt$eu7@masala.cc.uh.edu> <DpBuF6.83C@ukpsshp1.serigate.philips.nl>
  9. NNTP-Posting-Host: hd42-154.compuserve.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes) wrote:
  13.  
  14. >Spasmo (cosc19z5@Bayou.UH.EDU) wrote:
  15. >: Artur Wojdat (awojdat@lava.weeg.uiowa.edu) wrote:
  16. >: : Hello everybody,
  17. >: :     Is there a function or some sort of way that I could remove '\n' 
  18. >: : charecter form the end of the string. I'm reading from two files, want to 
  19. >: Dunno if there are any functions available, but what I always do
  20. >: is just overwrite the '\n' with a '\0', which does the job nicely.
  21.  
  22. >: For example, let's say that the string that holds the data is called
  23. >: buf.  To get rid of the '\n' you'd merely do the following:
  24.  
  25. >:     buf[strlen(buf) - 1] = '\0';
  26.  
  27. >: And that takes care of that.
  28.  
  29. >Thats the best way, but do check that the character you are about to overwrite
  30. >is a newline. There are two occasions when fgets stops without reading a 
  31. >newline, first if it has filled your buffer (because you have a very long line
  32. >in the file), second if there is no newline at the end of the last line of the
  33. >file.
  34.  
  35. >--
  36. >Stephen Baynes                              baynes@ukpsshp1.serigate.philips.nl
  37. >Philips Semiconductors Ltd
  38. >Southampton                                 My views are my own.
  39. >United Kingdom
  40.  
  41. Try this
  42.  
  43. char * ptr;
  44.  
  45. if ((ptr = strchr (buf, '\n')) != NULL) *ptr = '\0';
  46.  
  47.  
  48.